<?php
// Active assert and make it quiet
//assert_options(ASSERT_ACTIVE, 1);
//assert_options(ASSERT_WARNING, 0);
//assert_options(ASSERT_QUIET_EVAL, 1);

// Create a handler function
/*function my_assert_handler($file, $line, $code)
{
    echo "<hr>Assertion Failed:
        File '$file'<br />
        Line '$line'<br />
        Code '$code'<br /><hr />";
}*/

// Set up the callback
//assert_options(ASSERT_CALLBACK, 'my_assert_handler');



define( "ARROW_CACHE_PATH", "./cache" );
define( "ARROW_PROJECTS_PATH", "./" );
define( "ARROW_LOG_PATH", "./log" );

define( "ARROW_ABSOLUTE_PATH", dirname(__FILE__)."/../server"); 
define( "ARROW_CORE_PATH", ARROW_ABSOLUTE_PATH."/core" );
require_once(ARROW_CORE_PATH."/ArrowServerInit.php");

ArrowServerInit::init();


ArrowLogger::log("[Main] Starting..");

ArrowRouter::$INDEX_FILE = basename( __FILE__ ) ;

$controller = ArrowController::getDefault( );

try{
$router = ArrowRouter::getDefault( );
} catch( Excepiton $e ) {
print_r("aaaa");
exit;
}
if( ($mediator = $router->getMediator()) ){
  ArrowLogger::log("[Main] Mediator call checking...");
	
	if($mediator->getCheckResult(ArrowRequestContext::getDefault()) != true){
		
		ArrowLogger::log("[Main] Mediator returns false, must rollback request...", ArrowLogger::EL_INFO);
		
		if( $router->getCallType() == ArrowRouter::ACTION_BEAN_CALL || $router->getCallType() == ArrowRouter::TEMPLATE_CALL ){
			$controller->rollBackRequest();
		}elseif($router->getCallType() == ArrowRouter::REMOTE_CALL){
			ArrowLogger::log( "[Main] Remote mediator call, mediator returns false, returning error XML" );
			$remoteResponseHAndler = $controller::getDefault()->getHandler(ArrowProject::IRemoteResponseHandler);
			$remoteResponseHAndler->setHeaders();
			$response = $remoteResponseHAndler->getResponse(array( "error" => $mediator->getError() ));
			ArrowController::end($response);
		}
	}
	
	ArrowLogger::log( "[Main] Mediator returns true, executing request..." );
	
	//after check - static address
	$afterAddress = $mediator->getAfterAddress();
	
 	//after check go to view without mediator parametr
	if( $router->getCallType() == ArrowRouter::TEMPLATE_CALL){
		if( !empty($afterAddress) )
	    	$controller->redirectToStaticAddress($afterAddress);
		
		$controller->processToView();
	}

}

switch ( $router->getCallType() ){
	case ArrowRouter::ACTION_BEAN_CALL:
		$actionBean = $router->getActionBean();
		$instance = $actionBean->getBeanInstance();
		if( $instance instanceof IArrowBeanMediator  && $instance->getCheckResult(ArrowRequestContext::getDefault()) != true )
			$controller->rollBackRequest();	
		$controller->executeBean( $actionBean );
		
	    if( isset($afterAddress) && !empty($afterAddress) )
	    	$controller->redirectToStaticAddress($afterAddress);
	    	
		
		$controller->processToView();
	break;
	case ArrowRouter::REMOTE_CALL:
		$dataProviderBean = $router->getDataProviderBean();
		$instance = $dataProviderBean->getBeanInstance();
		if( $instance instanceof IArrowBeanMediator  && $instance->getCheckResult(ArrowRequestContext::getDefault()) != true ){
			$remoteResponseHAndler = $contriller->getProject()->getHandler(ArrowProject::IRemoteResponseHandler);
			$remoteResponseHAndler->setHeaders();
			$response = $remoteResponseHAndler->getResponse($instance->getError());
			ArrowController::end($response);	
		}	
		$controller->remoteResponse( $dataProviderBean );
	break;
	case ArrowRouter::TEMPLATE_CALL:
		
		if( count(ArrowRequestContext::getDefault()->getPost()) > 0)
			$controller->processToView();
		$pageClass = $router->getTemplate()->getPageClass();
		$pageClass->createPage();
		$pageClass->createView();
		$pageClass->controlsInit();
		$pageClass->afterControlsInit();
		echo $pageClass->display();
		
	break;	
	case ArrowRouter::CONTROLL_CALL:
		$pageClass = $router->getTemplate()->getPageClass();
		$pageClass->createPage();
		$pageClass->createView();
		$pageClass->controlsInit();
		$pageClass->afterControlsInit();
		echo $pageClass->generateControll();
	break;	
	case ArrowRouter::TEMPLATE_METHOD_CALL:
    $request = ArrowRequestContext::getDefault();
    $pageClass = $router->getTemplate()->getPageClass();
		$pageClass->createPage();
		$pageClass->createView();
		$pageClass->controlsInit();
		$pageClass->afterControlsInit();
		$method = $request->getRequest(ArrowRouter::TEMPLATE_METHOD_PARAMETER);
		
		$data = $request->getRequest( "methodCallData" ) ;
		$data = ( is_array($data) )? $data: array() ;
		$result = call_user_func_array  ( array( $pageClass, $method),  $data  );
  	$remoteResponseHAndler = $controller->getProject()->getHandler(ArrowProject::IRemoteResponseHandler);
  	$response = $remoteResponseHAndler->getResponse($result);
		ArrowController::end($response);	

	break;
	
	
}
ArrowController::end();
?>